package org.iabako.shared.entity.user;

import com.google.gwt.user.client.rpc.GwtTransient;
import org.iabako.shared.entity.business.AbstractEntity;
import org.iabako.shared.entity.enumeration.IabakoPackage;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jose on 16/12/14.
 */

@Entity
@Table(name = "USER_PAYPAL_TOKEN")
public class UserPayPalToken extends AbstractEntity {

    @Id
    @GeneratedValue
    @Column(name = "ID")
    private Long id;

    @ManyToOne
    @JoinColumn(name = "ID_PRINCIPAL_USER", nullable = false)
    @NotNull
    private User principalUser;

    @ManyToOne
    @JoinColumn(name = "ID_PAYER", nullable = false)
    @NotNull
    private User payer;

    @Column(name = "TX_ID", unique = true)
    private String transactionId;

    @Column(name = "PACKAGE")
    @Enumerated(EnumType.STRING)
    private IabakoPackage iabakoPackage;

    @Column(name = "QUANTITY", nullable = false)
    private int quantity;

    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "userPayPalToken")
    //To Avoid serializing exception in lazy/proxy objects.
    @GwtTransient
    private List<UserPayPalTokenUser> userTokens;

    @Column(name = "UPGRADE_CREDIT", nullable = false)
    private boolean upgradeCredit;

    @Column(name = "PAYED", nullable = false)
    private boolean payed;

    @Column(name = "CANCELED", nullable = false)
    private boolean canceled;

    @Column(name = "TO_VERIFY", nullable = false)
    private boolean toVerify;

    @Column(name = "PAYPAL_RESPONSE", length = 1000)
    private String payPalResponse;

    @Column(name = "PAYPAL_PAYER_EMAIL")
    private String payPalPayerEmail;

    @Column(name = "DATE", nullable = false)
    @Temporal(value = TemporalType.TIMESTAMP)
    private Date date;

    @Column(name = "AMOUNT", nullable = false)
    private Double amount;

    //This is for PayPalForm
    @Transient
    private List<UserPayPalTokenUser> userTokensTransient;

    @Override
    public void copyDatesAsString() {
    }

    public UserPayPalToken() {
        date = new Date();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public User getPrincipalUser() {
        return principalUser;
    }

    public void setPrincipalUser(User principalUser) {
        this.principalUser = principalUser;
    }

    public User getPayer() {
        return payer;
    }

    public void setPayer(User payer) {
        this.payer = payer;
    }

    public String getTransactionId() {
        return transactionId;
    }

    public void setTransactionId(String transactionId) {
        this.transactionId = transactionId;
    }

    public IabakoPackage getIabakoPackage() {
        return iabakoPackage;
    }

    public void setIabakoPackage(IabakoPackage iabakoPackage) {
        this.iabakoPackage = iabakoPackage;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public List<UserPayPalTokenUser> getUserTokens() {
        return userTokens;
    }

    public void setUserTokens(List<UserPayPalTokenUser> userTokens) {
        this.userTokens = userTokens;
    }

    public boolean isUpgradeCredit() {
        return upgradeCredit;
    }

    public void setUpgradeCredit(boolean upgradeCredit) {
        this.upgradeCredit = upgradeCredit;
    }

    public boolean isPayed() {
        return payed;
    }

    public void setPayed(boolean payed) {
        this.payed = payed;
    }

    public boolean isCanceled() {
        return canceled;
    }

    public void setCanceled(boolean canceled) {
        this.canceled = canceled;
    }

    public boolean isToVerify() {
        return toVerify;
    }

    public void setToVerify(boolean toVerify) {
        this.toVerify = toVerify;
    }

    public String getPayPalResponse() {
        return payPalResponse;
    }

    public void setPayPalResponse(String payPalResponse) {
        this.payPalResponse = payPalResponse;
    }

    public String getPayPalPayerEmail() {
        return payPalPayerEmail;
    }

    public void setPayPalPayerEmail(String payPalPayerEmail) {
        this.payPalPayerEmail = payPalPayerEmail;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public List<UserPayPalTokenUser> getUserTokensTransient() {
        if (userTokensTransient == null) {
            userTokensTransient = new ArrayList<UserPayPalTokenUser>();
        }
        return userTokensTransient;
    }
}
